//
//  AudioToolBar.m
//  StupidFM
//
//  Created by 寒竹子 on 15/5/28.
//  Copyright (c) 2015年 寒竹子. All rights reserved.
//

// 播放器播放状态发生变化的通知
#define KNotification_PlayerStateChanged @"PlayerStateChangedNotification"

#define KBorder    20.0f
#define KPlayBtn_W 35.0f
#define KImage_W   45.0f

#import "AudioToolBar.h"
#import "UIImageView+WebCache.h"
#import "Globle.h"
#import "XRAudioProgressButton.h"

@interface AudioToolBar ()

@property (nonatomic, strong) XRAudioProgressButton * playBtn;
@property (nonatomic, strong) UIImageView * headImage;
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) Globle * globle;
@property (nonatomic, strong) CADisplayLink * link;

@end

@implementation AudioToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePlayButtonState) name:KNotification_PlayerStateChanged object:nil];
        
        // 打开用户交互
        self.userInteractionEnabled = YES;
        [self setImage:[UIImage imageNamed:@"footbar_bg"]];
        
        // 全局属性
        _globle = [Globle sharedGloble];
        
        // 播放按钮
        _playBtn = [[XRAudioProgressButton alloc] initWithFrame:CGRectMake(KBorder, (frame.size.height - KPlayBtn_W) / 2.0f, KPlayBtn_W, KPlayBtn_W) backColor:RGB(255, 255, 255, 1.0f) progressColor:RGB(71, 194, 116, 1.0f) lineWidth:3.0f];
        _playBtn.layer.masksToBounds = YES;
        _playBtn.layer.cornerRadius = KPlayBtn_W / 2.0f;
        
        if (_globle.isPlaying) {
            // 暂停更新进度
            [_playBtn stopTimer];
            [_playBtn setImage:[UIImage imageNamed:@"play-gray"] forState:UIControlStateNormal];
            [_playBtn setImage:[UIImage imageNamed:@"play-gray_h"] forState:UIControlStateHighlighted];
        }else {
            // 开始更新进度
            [_playBtn startTimer];
            [_playBtn setImage:[UIImage imageNamed:@"pause-gray"] forState:UIControlStateNormal];
            [_playBtn setImage:[UIImage imageNamed:@"pause-gray_h"] forState:UIControlStateHighlighted];
        }
        
        [_playBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_playBtn];
        
        // 专辑图片
        _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width - KBorder - KImage_W, (frame.size.height - KImage_W) / 2.0f, KImage_W, KImage_W)];
        _headImage.layer.masksToBounds = YES;
        _headImage.layer.cornerRadius = KImage_W / 2.0f;
        [_headImage sd_setImageWithURL:[NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201405/14/20140514220629_BxG3H.jpeg"] placeholderImage:[UIImage imageNamed:@""]];
        [self addSubview:_headImage];
        
        // 标题
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_playBtn.frame) + KBorder, 0, frame.size.width - CGRectGetMaxX(_playBtn.frame) - KBorder * 3.0f - KImage_W, frame.size.height)];
        _titleLabel.font = TextFontWithSize(14.0f);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = RGBCOLOR(59, 59, 59, 1.0f);
        _titleLabel.text = @"创业者应有的5个正常心态";
        [self addSubview:_titleLabel];
    }
    
    return self;
}

+ (instancetype)sharedAudioToolBarWithFrame:(CGRect)frame
{
    static AudioToolBar * audioToolBar = nil;
    
    dispatch_once_t once = 0l;
    dispatch_once(&once, ^{
        if (audioToolBar == nil) {
            audioToolBar = [[AudioToolBar alloc] initWithFrame:frame];
        }
    });
    
    return audioToolBar;
}

/**
 * @brief  CADisplayLink
 *
 * @param
 *
 * @return
 */
- (CADisplayLink *)link
{
    if (nil == _link) {
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateLink)];
    }
    
    return _link;
}

/**
 * @brief  头像旋转
 *
 * @param
 *
 * @return
 */
- (void)updateLink
{
    // 1s 22.5度
    CGFloat angle = _link.duration * M_PI_4 / 2.0f;
    _headImage.transform = CGAffineTransformRotate(_headImage.transform, angle);
}

/**
 * @brief  更新头像旋转
 *
 * @param
 *
 * @return
 */
- (void)updateHeadRatation
{
    if (_globle.isPlaying) {
        [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    }else {
        [self.link invalidate];
        self.link = nil;
    }
}

/**
 * @brief  播放按钮Action
 *
 * @param
 *
 * @return
 */
- (void)playBtnAction:(UIButton *)btn
{
    if (!_globle.isHasResource) {
        // 没有可供播放资源
        NSLog(@"没有可供播放的音乐资源");
        return;
    }
    if (_globle.isPlaying) {
        _globle.isPlaying = NO;
        [_playBtn stopTimer]; // 停止进度显示
        // 正在播放
        [btn setImage:[UIImage imageNamed:@"pause-gray"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"pause-gray_h"] forState:UIControlStateHighlighted];
    }else {
        _globle.isPlaying = YES;
        [_playBtn startTimer]; // 开始更新进度显示
        // 暂停播放
        [btn setImage:[UIImage imageNamed:@"play-gray"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"play-gray_h"] forState:UIControlStateHighlighted];
    }

    [self updateHeadRatation];
    // 代理处理
    if (_delegate && [_delegate respondsToSelector:@selector(playAudioWithButtonState:)]) {
        [_delegate playAudioWithButtonState:_globle.isPlaying];
    }
}

#pragma mark - private Monthods

/**
 * @brief  更新按钮的状态
 *
 * @param
 *
 * @return
 */
- (void)updatePlayButtonState
{
    if (_globle.isPlaying) {
        // 暂停进度更新
        [_playBtn stopTimer];
        [_playBtn setImage:[UIImage imageNamed:@"pause-gray"] forState:UIControlStateNormal];
        [_playBtn setImage:[UIImage imageNamed:@"pause-gray_h"] forState:UIControlStateHighlighted];
    }else {
        // 开始更新进度显示
        [_playBtn startTimer];
        [_playBtn setImage:[UIImage imageNamed:@"play-gray"] forState:UIControlStateNormal];
        [_playBtn setImage:[UIImage imageNamed:@"play-gray_h"] forState:UIControlStateHighlighted];
    }
    // 更新头像状态
    [self updateHeadRatation];
}

@end
